package 尚硅谷juc大厂.test17;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.concurrent.atomic.LongAdder;

class ClickNumber{



    /**
     * 模拟点赞：
     * 使用synchronized
     */
    int num=0;
    public synchronized void add_synchronized(){
        num++;
    }

    /**
     * 模拟点赞：
     * 使用AtomicInteger原子整型
     */
    AtomicInteger atomicInteger=new AtomicInteger();
    public void add_atomicInteger(){
        atomicInteger.incrementAndGet();
    }
    /**
     * 模拟点赞：
     * 使用AtomicLong原子长整型
     */
    AtomicLong atomicLong=new AtomicLong();
    public void add_atomicLong(){
        atomicLong.incrementAndGet();
    }

    /**
     * 模拟点赞：
     * 使用longAdder版本
     * 阿里巴巴推荐使用LongAdder
     */
    LongAdder longAdder=new LongAdder();
    public void longAdder(){
        longAdder.increment();
    }

    /**
     * 模拟点赞：
     * 使用LongAccumulator版本
     */
    LongAccumulator longAccumulator=new LongAccumulator((x,y)->x+y,0);
    public void add_longAccumulator(){
        longAccumulator.accumulate(1);
    }

}

public class Test17 {

    public static void main(String[] args) throws InterruptedException {
        ClickNumber clickNumber=new ClickNumber();
        long start;
        long end;

        /**
         * add_synchronized版本
         */
        start=System.currentTimeMillis();
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for (int i=0;i<50;i++){
            new Thread(()->{
                try {
                    for (int j=0;j<100*10000;j++){
                        clickNumber.add_synchronized();
                    }
                }catch (Exception exception){

                }finally {
                    countDownLatch.countDown();
                }

            },String.valueOf(i)).start();

        }
        countDownLatch.await();
        end=System.currentTimeMillis();
        System.out.println("synchronized版本耗时："+(end-start)+"毫秒"+"，最终点赞结果是："+clickNumber.num);


        /**
         * add_synchronized版本
         */
        start=System.currentTimeMillis();
        CountDownLatch add_atomicInteger = new CountDownLatch(50);
        for (int i=0;i<50;i++){
            new Thread(()->{
                try {
                    for (int j=0;j<100*10000;j++){
                        clickNumber.add_atomicInteger();
                    }
                }catch (Exception exception){

                }finally {
                    add_atomicInteger.countDown();
                }

            },String.valueOf(i)).start();

        }
        add_atomicInteger.await();
        end=System.currentTimeMillis();
        System.out.println("atomicInteger版本耗时："+(end-start)+"毫秒"+"，最终点赞结果是："+clickNumber.atomicInteger.get());

        /**
         * add_atomicLong版本
         */
        start=System.currentTimeMillis();
        CountDownLatch add_atomicLong = new CountDownLatch(50);
        for (int i=0;i<50;i++){
            new Thread(()->{
                try {
                    for (int j=0;j<100*10000;j++){
                        clickNumber.add_atomicLong();
                    }
                }catch (Exception exception){

                }finally {
                    add_atomicLong.countDown();
                }

            },String.valueOf(i)).start();

        }
        add_atomicLong.await();
        end=System.currentTimeMillis();
        System.out.println("atomicLong版本耗时："+(end-start)+"毫秒"+"，最终点赞结果是："+clickNumber.atomicLong.get());


        /**
         * longAdder版本
         */
        start=System.currentTimeMillis();
        CountDownLatch longAdder = new CountDownLatch(50);
        for (int i=0;i<50;i++){
            new Thread(()->{
                try {
                    for (int j=0;j<100*10000;j++){
                        clickNumber.longAdder();
                    }
                }catch (Exception exception){

                }finally {
                    longAdder.countDown();
                }

            },String.valueOf(i)).start();

        }
        longAdder.await();
        end=System.currentTimeMillis();
        System.out.println("longAdder版本耗时："+(end-start)+"毫秒"+"，最终点赞结果是："+clickNumber.longAdder.longValue());


        /**
         * longAdder版本
         */
        start=System.currentTimeMillis();
        CountDownLatch add_longAccumulator = new CountDownLatch(50);
        for (int i=0;i<50;i++){
            new Thread(()->{
                try {
                    for (int j=0;j<100*10000;j++){
                        clickNumber.add_longAccumulator();
                    }
                }catch (Exception exception){

                }finally {
                    add_longAccumulator.countDown();
                }

            },String.valueOf(i)).start();

        }
        add_longAccumulator.await();
        end=System.currentTimeMillis();
        System.out.println("longAccumulator版本耗时："+(end-start)+"毫秒"+"，最终点赞结果是："+clickNumber.longAccumulator.longValue());

    }


}
